package com.expense.common.gen;

import cn.hutool.core.util.StrUtil;
import org.beetl.core.Template;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.db.TableDesc;
import org.beetl.sql.core.kit.GenKit;
import org.beetl.sql.ext.gen.GenConfig;
import org.beetl.sql.ext.gen.MapperCodeGen;
import org.beetl.sql.ext.gen.SourceGen;

import java.io.IOException;

public class CommonCodeGen extends MapperCodeGen {
    String pkg = null;
    private String mapperTemplate = "";
    private String classNameSuffix = "";//类名,同时也是文件名的后缀,例如AdminDao->Dao就是后缀
    private String tableRemark = "";//类标题,例如:AdminUser->用户
    private Boolean isCreateFolder = false;//是否创建子目录文件夹

    /**
     * 构造函数
     * @param pkg 包名
     * @param template 模板路径
     * @param suffix 类名/文件名后缀
     */
    public CommonCodeGen(String pkg,String template, String suffix) {
        this.pkg = pkg;
        this.mapperTemplate = (new GenConfig()).getTemplate(template);
        this.classNameSuffix = suffix;
    }
    /**
     * 构造函数
     * @param pkg 包名
     * @param template 模板路径
     * @param suffix 类名/文件名后缀
     */
    public CommonCodeGen(String pkg,String template, String suffix,String tableRemark) {
        this.pkg = pkg;
        this.mapperTemplate = (new GenConfig()).getTemplate(template);
        this.classNameSuffix = suffix;
        this.tableRemark = tableRemark;
    }

    @Override
    public String getMapperTemplate() {
        return mapperTemplate;
    }

    @Override
    public void setMapperTemplate(String mapperTemplate) {
        this.mapperTemplate = mapperTemplate;
    }

    public void setClassNameSuffix(String suffix){
        this.classNameSuffix = suffix;
    }

    public void genCode(SQLManager sqlManager,String tableName, Boolean isCreateFolder){
        this.isCreateFolder = isCreateFolder;
        String entityClass = StrUtil.upperFirst(StrUtil.toCamelCase(tableName));
        TableDesc tableDesc = sqlManager.getMetaDataManager().getTable(tableName);
        genCode(this.pkg,entityClass,tableDesc,null,false);
    }
    public void genCode(SQLManager sqlManager,String tableName){
        String entityClass = StrUtil.upperFirst(StrUtil.toCamelCase(tableName));
        TableDesc tableDesc = sqlManager.getMetaDataManager().getTable(tableName);
        genCode(this.pkg,entityClass,tableDesc,null,false);
    }
    @Override
    public void genCode(String entityPkg, String entityClass, TableDesc tableDesc, GenConfig config, boolean isDisplay) {
        if (this.pkg == null) {
            this.pkg = entityPkg;
        }
        String camelClassName = StrUtil.lowerFirst(entityClass);
        if(this.isCreateFolder) {
            this.pkg += "." + camelClassName;
        }
        if(StrUtil.isBlank(this.tableRemark)) this.tableRemark = tableDesc.getRemark();
        Template template = SourceGen.getGt().getTemplate(this.mapperTemplate);
        template.binding("className", entityClass);
        template.binding("camelClassName", camelClassName);
        template.binding("package", this.pkg);
        template.binding("comment", this.tableRemark);
        String mapperHead = "import " + entityPkg + ".*;" + SourceGen.CR;
        template.binding("imports", mapperHead);
        String mapperCode = template.render();
        if (isDisplay) {
            System.out.println(mapperCode);
        } else {
            try {
              String path = GenKit.getJavaSRCPath();
                SourceGen.saveSourceFile(path, this.pkg, entityClass+this.classNameSuffix, mapperCode);
            } catch (IOException var11) {
                throw new RuntimeException("mapper代码生成失败", var11);
            }
        }

    }
}
